package com.java.mogodb;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Projections;
import org.bson.Document;
import static com.mongodb.client.model.Filters.*;

public class LogicalOperations {
    private static final String collectionName = "StudentDetails";
private  static  final  String dataConnect="LogicalOperations";
    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient("mongodb://localhost:27017");
        MongoDatabase database = mongoClient.getDatabase(dataConnect); // Use correct DB name

        MongoCollection<Document> collection =database.getCollection(collectionName);


        //Find the student with _id = 4.
        System.out.println("Find the student with _id = 4.");
        collection.find(eq("_id",4)).forEach(System.out::println);


        //List all students who are active (active = true).
        System.out.println("List all students who are active (active = true).");
        collection.find(eq("active",true)).forEach(System.out::println);


        //Retrieve students whose score is greater than 90.
        System.out.println("Retrieve students whose score is greater than 90.");
        collection.find(gte("score",90)).forEach(System.out::println);


        //Find all students from China.
        System.out.println("Find all students from China.");
        collection.find(eq("country","China")).forEach(System.out::println);


        //List students where the country field exists.
        System.out.println("List students where the country field exists.");
        collection.find(exists("country",true)).forEach(System.out::println);


        //Retrieve documents where the rating field is missing or null.
        System.out.println("Retrieve documents where the rating field is missing or null.");
        collection.find(or(eq("rating",null),eq("rating",null))).forEach(System.out::println);

        //Find students who study Math.
        System.out.println("Find students who study Math.");
        collection.find(in("subjects","Msth")).forEach(System.out::println);

        //Retrieve students who have both Physics and Chemistry in their subjects.
        System.out.println("Retrieve students who have both Physics and Chemistry in their subjects.");
        collection.find(in("subjects","Physics","Chemistry")).forEach(System.out::println);

        //List students with exactly three subjects.
        System.out.println("List students with exactly three subjects.");
        collection.find(size("subjects",3)).forEach(System.out::println);

        //Find students who are under 25 and have score > 85.
        System.out.println("Find students who are under 25 and have score > 85.");
        collection.find(and(lt("age",25),gte("source",85))).forEach(System.out::println);

        //Retrieve students who are either from London or have a score greater than 90.
        System.out.println("Retrieve students who are either from London or have a score greater than 90.0");
        collection.find(or(eq("City","London"),gte("score",90))).forEach(System.out::println);

        //Find students from China with rating > 15.
        System.out.println("ind students from China with rating > 15.");
        collection.find(and(eq("country","China"),gt("rating",15))).forEach(System.out::println);

        //Sort students by score in descending order and show the top 3.
        System.out.println("Sort students by score in descending order and show the top 3.");
        collection.find().limit(3).sort(new Document("score",-1)).forEach(System.out::println);

        //List all students ordered by age in ascending order.
        System.out.println("List all students ordered by age in ascending order.");
        collection.find().sort(new Document("age",1)).forEach(System.out::println);

        //Retrieve only the name and city of students from Asia (where country = "China").
        System.out.println("Retrieve only the name and city of students from Asia (where country = \"China\").");
        collection.find(eq("country", "China"))
                .projection(Projections.fields(Projections.include("name", "city")))
                .forEach(System.out::println);

        //List students with exactly three subjects.
        System.out.println("List students with exactly three subjects.");
        collection.find(size("subjects",3)).forEach(System.out::println);
mongoClient.close();
    }

    }

